package com.soup.bytebuddy;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.util.concurrent.Callable;

import static net.bytebuddy.dynamic.loading.ClassLoadingStrategy.Default.INJECTION;
import static net.bytebuddy.implementation.MethodDelegation.to;
import static net.bytebuddy.matcher.ElementMatchers.any;

/**
 * 〈一句话功能简述〉<br>
 *     https://stackoverflow.com/questions/31264748/how-to-create-a-default-constructor-with-byte-buddy
 *
 *     https://www.thinbug.com/q/47265835
 *
 * @author zhaoyi
 * @date 2021-04-11 21:05
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class ConstructorIntercept {
    public static void main(String[] args) {
        new ByteBuddy().subclass (Object.class)
                .name ("com.soup.Foo")
                .constructor(any())
                .intercept(to(new Object () {
                    public void construct() throws Exception {
                        System.out.println("CALLING XTOR");
                    }
                }).andThen(SuperMethodCall.INSTANCE))
                .make()
                .load(ConstructorIntercept.class.getClassLoader (), INJECTION)
                .getLoaded ();
    }
}
